home *** CD-ROM | disk | FTP | other *** search
/ Workbench Add-On / Workbench Add-On - Volume 1.iso / Dev / Amiga-E / E_v3.2a / Src / Various / Pi.e < prev    next >
Text File  |  1992-09-02  |  2KB  |  80 lines

  1. /* Another pi-calc program.
  2.    A good example of what optimizing using inline assembly can do:
  3.    The E source is a translation of the original C source,
  4.    which did 48 seconds on 250 decimals, the E version did 30 seconds.
  5.    Then, the innermost loop was translated to inline assembly,
  6.    this version (E+Asm) timed only 10 seconds (all on 7mhz). 
  7.    Only a small part needed to be translated to assembly,
  8.    as that is where 99% of the calculation is performed   */
  9.  
  10. DEF m,k,n,p,i,max,nr,handle,out,num[50]:STRING,a:PTR TO LONG
  11.  
  12. PROC main()
  13.   WriteF('PI calc\n#of decimals (try 50-250): ')
  14.   ReadStr(stdout,num)
  15.   IF (nr:=Val(num,NIL))=0 THEN stop('Illegal #!\n')
  16.   WriteF('Busy ... press CtrlC to abort.\n\n')
  17.   max:=nr*16
  18.   IF (a:=New(max))=NIL THEN stop('No mem!\n')
  19.   m:=nr
  20.   k:=m!*3.321-1!
  21.   WriteF('\d\c',k,13)
  22.   FOR n:=k TO 1 STEP -1
  23.     a[0]:=a[0]+2
  24.     p:=n*2+1
  25.     MOVEQ   #0,D7    /* D7=c */
  26.     MOVE.L  a,A0    /* A0=a array */
  27.     MOVE.L  m,D4    /* D4=i counter */
  28.     MOVE.L  p,D2
  29.     MOVE.L  n,D3
  30. l:  MOVE.L  D7,D0    /* this loop is hyper-optimized. */
  31.     LSL.L   #3,D0
  32.     ADD.L   D7,D0    /* the following is the original E equivalent */
  33.     ADD.L   D7,D0
  34.     MOVE.L  (A0),D1    /* c:=0               */
  35.     MULU    D3,D1    /* x:=a               */
  36.     ADD.L   D1,D0    /* FOR i:=0 TO m      */
  37.     DIVU    D2,D0    /*   c:=10*c+(n*^x)   */
  38.     MOVE.L  D0,D7    /*   ^x:=c/p          */
  39.     EXT.L   D0        /*   c:=c-(^x*p)      */
  40.     MOVE.L  D0,(A0)    /*   x:=x+4           */
  41.     SWAP    D7        /* ENDFOR             */
  42.     EXT.L   D7
  43.     ADDQ.L  #4,A0
  44.     DBRA    D4,l
  45.     IF (n AND $F)=0     /* not every loop */
  46.       WriteF('\d     \c',n,13)
  47.       IF CtrlC() THEN stop('\n*** Calculation interrupted!\n')
  48.     ENDIF
  49.   ENDFOR
  50.   FOR i:=m TO 1 STEP -1
  51.     IF a[i]>9
  52.       a[i]:=a[i]-10
  53.       a[i-1]:=a[i-1]+1
  54.     ENDIF
  55.   ENDFOR
  56.   handle:=Open('ram:pi.txt',1006)
  57.   IF handle<>NIL
  58.     out:=SetStdOut(handle)
  59.     writenum()
  60.     SetStdOut(out)
  61.     Close(handle)
  62.     WriteF('\n\nSee ram:pi.txt for output.\n')
  63.   ELSE
  64.     WriteF('Could not open file!\n')
  65.   ENDIF
  66.   WriteF('\n')
  67.   writenum()
  68. ENDPROC
  69.  
  70. PROC stop(messy)
  71.   WriteF(messy)
  72.   CleanUp(5)
  73. ENDPROC
  74.  
  75. PROC writenum()
  76.   WriteF('pi=3.')
  77.   FOR i:=1 TO m DO WriteF('\d',a[i])
  78.   WriteF('\n')
  79. ENDPROC
  80.